package com.yonyou.brigade.third_parties.third.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.third_parties.third.model.BriUserAuth;
import com.yonyou.brigade.third_parties.third.dao.BriUserAuthMapper;
import com.yonyou.brigade.third_parties.third.service.IAuthUserUpdateObserver;
import com.yonyou.brigade.third_parties.third.service.IAuthUserUpdateObserverable;
import com.yonyou.brigade.third_parties.third.service.IBriUserAuthService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangzhf@yonyou.com
 * @since 2018-08-16
 */
@Service
public class BriUserAuthServiceImpl extends ServiceImpl<BriUserAuthMapper, BriUserAuth> implements IBriUserAuthService {

    @Autowired
    private BriUserAuthMapper briUserAuthMapper;

    @Autowired
    @Qualifier("authUserUpdateObserverable")
    private IAuthUserUpdateObserverable authUserUpdateObserverable;

    @Resource(name="ctripAuthUserUpdateObserver")
    private IAuthUserUpdateObserver ctripAuthUserUpdateObserver;


    public Map findUserAuthInfos(String orgpk, int pageSize, int current, String conditin, String setted)
    {
        Map result=new HashMap();
        List<BriUserAuth> resultUserAuths=new ArrayList<>();
        int total=0;
        if(current<=0)
        {
            current=1;
        }
        int start=(current-1)*pageSize;
        int size=pageSize;

        try {

            Map params=new HashMap();
            params.put("start",start);
            params.put("size",size);
            params.put("orgpk",orgpk);
            params.put("condition",conditin);
            params.put("setted",setted);
            resultUserAuths=briUserAuthMapper.selectUserAuthInfo(params);
            total=briUserAuthMapper.selectCountUserAuthInfo(params);
            result.put("code","0");
            result.put("information",resultUserAuths);
            JSONObject pagination=new JSONObject();
            pagination.put("total",total);
            pagination.put("pageSize",pageSize);
            pagination.put("current",current);
            result.put("pagination",pagination);
        }
        catch (Exception e)
        {
            result.put("code","1");
            result.put("information","操作失败");
            JSONObject pagination=new JSONObject();
            pagination.put("total",total);
            pagination.put("pageSize",pageSize);
            pagination.put("current",current);
            result.put("pagination",pagination);
        }

        return  result;
    }

    public Map updateUserAuth(List<BriUserAuth> userAuthList,String orgpk,List<String> userids,String destuserid)
    {
        Map result=new HashMap();
        if(userAuthList!=null&&userAuthList.size()>0)
        {
            authUserUpdateObserverable.removeObserver(ctripAuthUserUpdateObserver);
            authUserUpdateObserverable.registerObserver(ctripAuthUserUpdateObserver);
            Map tempResult =  authUserUpdateObserverable.notifyObserver(userids,orgpk,destuserid);
            if(!tempResult.get("code").equals("0"))
            {
                return tempResult;
            }
            for(int i=0;i<userAuthList.size();i++)
            {
                String curUserId=userAuthList.get(i).getUserId();
                Map params=new HashMap();
                params.put("orgpk",orgpk);
                params.put("userid",curUserId);
//                BriUserAuth userAuth=briUserAuthMapper.selectSingleUserAuth(params);
//                if(userAuth!=null)
//                {
//                    ctripUserMapper.updateUserAuthInfo(userAuthList.get(i));
//                }
//                else
//                {
//                    ctripUserMapper.insertUserAuthInfo(userAuthList.get(i));
//                }
            }
        }
        result.put("code","0");
        result.put("information","操作成功");
        return result;
    }


}
